package cx.zan.web.servlet;

import cx.zan.domain.Category;
import cx.zan.service.CategoryService;
import cx.zan.utils.UUIDUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 分类
 * @author Mxb
 * @version 1.0
 * @date 2020/4/1 10:04
 */
@WebServlet("/category")
public class CategoryServlet extends BaseServlet {
    private CategoryService categoryService = new CategoryService();

    /**
     * 查询所有 分类信息
     * @param request
     * @param response
     * @throws IOException
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Category> list = categoryService.findAll();
        writeSuccess(list);
    }

    /**
     * 查询单个分类信息
     * @param request
     * @param response
     * @throws IOException
     */
    public void findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cid = request.getParameter("cid");
        Category category = categoryService.findById(cid);
        writeSuccess(category);
    }

    /**
     * 删除单个分类信息
     * @param request
     * @param response
     * @throws IOException
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cid = request.getParameter("cid");
        categoryService.delete(cid);
        writeSuccess("删除成功！");
    }

    /**
     * 修改分类信息
     * @param request
     * @param response
     * @throws IOException
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String cname = request.getParameter("cname");
        String cid = request.getParameter("cid");
        Category category = new Category();
        category.setCname(cname);
        category.setCid(cid);

        categoryService.update(category);
        writeSuccess("修改成功！");
    }

    /**
     * 添加分类信息
     * @param request
     * @param response
     * @throws IOException
     */
    public void insert(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //创建分类对象
        Category category = new Category();
        String cname = request.getParameter("cname");
        category.setCname(cname);
        category.setCid(UUIDUtil.getId());
        categoryService.insert(category);

        writeSuccess("添加成功!");
    }
}
